'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 用户相关
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var Code = require('../../../consts/code');
var PlayerManager = require('../../../libs/playerManager');

module.exports = function (app) {
    return new Handler(app);
};

var Handler = function (app) {
    this.app = app;
};

Handler.prototype.getInfoByRoleId = function (msg, session, next) {
    var role_id = msg.query.role_id;
    if (!role_id) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['bag', 'accrued'],
                    cb
                );
            }],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    equip_list: player_manager.bag.getEquipList(),
                    property_list: player_manager.bag.getPropertyList()
                }
            });
        }
    );
};

/**
 * 添加装备
 */
Handler.prototype.addEquip = function (msg, session, next) {
    var role_id = msg.query.role_id;
    var equip_type = msg.query.equip_type;
    if (!role_id || !equip_type) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var player_manager = new PlayerManager(role_id);
    async.waterfall([
            function (cb) {
                player_manager.getModel(
                    ['bag', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var bag_model = player_manager.bag;
                if (!bag_model.addEquip(equip_type, true)) {
                    return next(null, {
                        code: Code.BAG_ITEM_NOT_EXIST
                    });
                }
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK
            });
        }
    );
};

/**
 * 添加道具
 */
Handler.prototype.addProperty = function (msg, session, next) {
    var role_id = msg.query.role_id;
    var property_type = msg.query.property_type;
    if (!role_id || !property_type) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var player_manager = new PlayerManager(role_id);
    async.waterfall([
            function (cb) {
                player_manager.getModel(
                    ['bag', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var bag_model = player_manager.bag;
                if (!bag_model.addProperty(property_type, 1, true)) {
                    return next(null, {
                        code: Code.BAG_ITEM_NOT_EXIST
                    });
                }
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK
            });
        }
    );
};

/**
 * 添加奖励消耗品
 */
Handler.prototype.addConsume = function (msg, session, next) {
    var role_id = msg.query.role_id;
    var consume_type = msg.query.consume_type;
    if (!role_id || !consume_type) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var player_manager = new PlayerManager(role_id);
    async.waterfall([
            function (cb) {
                player_manager.getModel(
                    ['bag', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var bag_model = player_manager.bag;
                if (!bag_model.addConsume(consume_type, 1, true)) {
                    return next(null, {
                        code: Code.BAG_ITEM_NOT_EXIST
                    });
                }
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK
            });
        }
    );
};
